import unittest
# 从主程序导入所有统计函数
from wc import (
    count_chars,
    count_words,
    count_sentences,
    count_lines,
    count_blank_lines,
    count_comment_lines,
    count_code_lines
)

class TestWcFunctions(unittest.TestCase):
    """完整测试所有统计函数的单元测试类"""

    # 测试字符数统计
    def test_count_chars(self):
        self.assertEqual(count_chars("Hello"), 5)
        self.assertEqual(count_chars("Hello World!"), 12)  # 含空格和标点
        self.assertEqual(count_chars("  \n\t"), 4)  # 空白字符
        self.assertEqual(count_chars(""), 0)  # 空字符串

    # 测试单词数统计
    def test_count_words(self):
        self.assertEqual(count_words("Hello World"), 2)
        self.assertEqual(count_words("Hello, world! 123"), 2)  # 含符号和数字
        self.assertEqual(count_words("   Python is great   "), 3)  # 前后空格
        self.assertEqual(count_words("12345!@#$%"), 0)  # 无有效单词
        self.assertEqual(count_words(""), 0)  # 空字符串

    # 测试句子数统计
    def test_count_sentences(self):
        self.assertEqual(count_sentences("Hi! How are you?"), 2)
        self.assertEqual(count_sentences("Hello!! Bye..."), 2)  # 连续标点
        self.assertEqual(count_sentences("No punctuation here"), 1)  # 无标点结尾
        self.assertEqual(count_sentences("A.B.C"), 3)  # 缩写句点
        self.assertEqual(count_sentences(""), 0)  # 空字符串

    # 测试总行数统计
    def test_count_lines(self):
        self.assertEqual(count_lines("line1\nline2\nline3"), 3)
        self.assertEqual(count_lines("single line"), 1)
        self.assertEqual(count_lines("\n\n"), 3)  # 空行+换行符
        self.assertEqual(count_lines(""), 1)  # 空字符串算1行

    # 测试空行数统计
    def test_count_blank_lines(self):
        self.assertEqual(count_blank_lines("a\n\n b \n\t"), 2)  # 第2行和第4行
        self.assertEqual(count_blank_lines("   \n"), 2)  # 两行空行
        self.assertEqual(count_blank_lines("abc\ndef"), 0)  # 无空行
        self.assertEqual(count_blank_lines(""), 1)  # 空字符串算1个空行

    # 测试注释行数统计（直接传lines列表）
    def test_count_comment_lines(self):
        lines1 = ["// 注释1", "code", "//comment2", "//   注释3  "]
        self.assertEqual(count_comment_lines(lines1), 3)

        lines2 = ["code // 行内注释", "  // 前有空格的注释", "normal line"]
        self.assertEqual(count_comment_lines(lines2), 1)  # 仅第2行是注释

        lines3 = ["", "   ", "// 混合空行和注释"]
        self.assertEqual(count_comment_lines(lines3), 1)  # 仅第3行

    # 测试代码行数统计（直接传lines列表）
    def test_count_code_lines(self):
        lines1 = [
            "// 注释行",
            "code1",
            "// 另一个注释",
            "",
            "code2 // 行内注释",
            "   "
        ]
        self.assertEqual(count_code_lines(lines1), 2)  # code1和code2

        lines2 = ["   code3   ", "", "// 注释", "code4"]
        self.assertEqual(count_code_lines(lines2), 2)  # code3和code4

        lines3 = ["", "// 纯注释文件", "   "]
        self.assertEqual(count_code_lines(lines3), 0)  # 无代码行

if __name__ == "__main__":
    # 运行所有测试并显示详细结果
    unittest.main(verbosity=2)